package com.lynn.salary.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.EmpRole;
import com.lynn.salary.entity.Permission;
import com.lynn.salary.entity.Role;
import com.lynn.salary.entity.RolePermission;
import com.lynn.salary.service.IRolePermissionService;
import com.lynn.salary.util.ToolMethod;

public class RolePermissionService extends BaseService implements IRolePermissionService{

	public Integer totalCount() throws Exception {
		String tn = ToolMethod.getTableName(Permission.class);
		String sql = "select count(*) tc from " + tn;
		List<Map<String, Object>> result = dao.executeSQL(sql);
		
		if(result != null && result.size() != 0) {
			Map<String,Object> map = result.get(0);
			Object o = map.get("tc");
			Integer i = Integer.parseInt(o.toString());
			return i;
		}
		return null;
	}

	public List<Map<String, Object>> findPermissionByLimit(Integer currentPage,
			Integer numPerPage) throws Exception {
		String tn = ToolMethod.getTableName(Permission.class);
		
		String sql = "select * from "+ tn + " limit "+(currentPage-1)*numPerPage + ","+numPerPage;
		
		return dao.executeSQL(sql);
	}
    
	public void save(Permission p) throws Exception {
		this.dao.save(p);
	}

	public void delete(String deleteId) throws Exception {
		this.dao.deleteById(Permission.class, deleteId);
	}

	public Permission findPermissionById(String editId) throws Exception {
		Permission p = dao.getById(Permission.class, editId);
		return p;
	}

	public void updatePermission(Permission permission) throws Exception {
		dao.update(permission);
	}

	public void deleteByCondition(String condition) throws Exception {
    	dao.delete(Permission.class, condition);
	}

	
	public void delete(String[] ids) throws Exception {
		StringBuffer c = new StringBuffer("id in (");
		
		for (int i = 0; i < ids.length; i++) {
			if(i!=0)c.append(",");
			c.append("'");
			c.append(ids[i]);
			c.append("'");
		}
		c.append(")");
		
		deleteByCondition(c.toString());
	}
	public Integer RtotalCount() throws Exception{
		String tn = ToolMethod.getTableName(Role.class);
		String sql = "select count(*) tc from " + tn;
		List<Map<String, Object>> result = dao.executeSQL(sql);
		
		if(result != null && result.size() != 0) {
			Map<String,Object> map = result.get(0);
			Object o = map.get("tc");
			Integer i = Integer.parseInt(o.toString());
			return i;
		}
		return null;
		
	}
	public List<Map<String,Object>> findRoleByLimit(Integer currentPage,Integer numPerPage) throws Exception{
		
			String tn = ToolMethod.getTableName(Role.class);
			
			String sql = "select * from "+ tn + " limit "+(currentPage-1)*numPerPage + ","+numPerPage;
			
			return dao.executeSQL(sql);
	}
	public void save(Role r) throws Exception{
				this.dao.save(r);
		
	}
	public void deleteRole(String deleteId) throws Exception{
		this.dao.deleteById(Role.class, deleteId);	
	}
	
	
	public void deleteRole(String[]ids) throws Exception{
		 StringBuffer c = new StringBuffer("id in (");
		for (int i = 0; i < ids.length; i++) {
			if(i!=0)c.append(",");
			c.append("'");
			c.append(ids[i]);
			c.append("'");
		}
		c.append(")");
		
		deleteByConditionRole(c.toString());
	}
	public void deleteByConditionRole(String condition) throws Exception{
		dao.delete(Role.class, condition);
	}
	
	public Role findRoleById(String editId) throws Exception{
		Role r = dao.getById(Role.class, editId);
		return r;
	}
	
	public void updateRole(Role role) throws Exception{
		dao.update(role);
		
	}
	
	public List<Map<String,Object>> findEmpContainRole(String roleId) throws Exception {
		
		return dao.getPartList(EmpRole.class, new String[]{"empId"}, "roleId='"+roleId+"'");
		
	}
}
